-- 计算玩家攻击力

-- 其实是取出战宠物攻击力
-- @param user    玩家对象
-- @param args    是否包含削弱属性
return function(user, args)
    local attack = user.dbase:query("attack", 1);
    local addon  = 0;

    local combine = PropM.combine;
    local apply = PropM.apply;
    local fetchProps = PropM.fetchProps;
    local trigger = PropM.trigger;

    -- --------------- 基础部分 ------------------------------------------
    -- --------------- 主要包括天赋的提升，和装备、技能的绝对值提升 ---------
    -- 攻击+N
    local prop = combine(user, "attrib2", "attack");
    attack = apply(prop, attack);

    -- 按阵营攻击加
    local classId = user.dbase:query("active_pet", 0)
    local campId = CampM.getPetCamp(classId)
    prop = combine(user, "camp_attack", campId);
    attack = apply(prop, attack);

    -- 按系别攻击加
    local departmentId = PetM.query(classId, "department_order")[1];
    prop = combine(user, "department_attack", departmentId);
    attack = apply(prop, attack);

    -- 贤者之语加成
    local stoneProp = ME.user.dbase:query("stone_prop", {});
    attack = attack + (stoneProp["attack"] or 0);

    -- 魔藤辅助
    local bine = user.bine;
    local bineAddon = user.dbase:query("bine_addon");
    if  type(bineAddon) == "table" and bine then
        attack = attack + (bineAddon["attack"] or 0);
    end

    -- 机械蜘蛛辅助
    local grids = SummonM.findSummonsByClassId(53);
    if #grids > 0 then
        local grid = grids[1];
        local summon = grid.summon;
        if summon and not summon:isDead() then
            prop = PropM.combine(summon, "attrib_to_user", 1);
            local summonAttack = summon:queryAttrib("attack") or 0;
            attack = attack + FormulaM.invoke("CALC_BIG_DIVISION", summonAttack, prop[3], 1000);
        end
    end

    -- 特定冈布奥攻击+
    local props = fetchProps(user, "slime_attack");
    for _, prop in ipairs(props) do
        if tonumber(prop[2]) == classId then
            attack = apply(prop, attack);
        end
    end

    -- 攻击+,特定冈布奥效果翻倍
    props = fetchProps(user, "slime_attack2");
    for _, prop in ipairs(props) do
        attack = apply(prop, attack);
        if tonumber(prop[2]) == classId  then
            attack = apply(prop, attack);
        end
    end

    -- 每多一个敌人，攻击+
    props = fetchProps(user, "sparta");
    for _, prop in ipairs(props) do
        -- 加攻击的
        if prop[2] == "attack" then
            attack = trigger(user, prop[1], "attack", attack);
        end
    end

    -- 攻击+,角斗场效果翻倍
    prop = combine(user, "wrestling_attrib", "attack");
    attack = apply(prop, attack);
    if ArenaM.isInArena() then
        attack = apply(prop, attack);
    end

    -- 角斗台属性增加
    if ArenaM.isInArena() then
        prop = combine(user, "attrib_arena", "attack");
        attack = apply(prop, attack);
    end

    -- 每增加一定生命，攻击提升
    props = fetchProps(user, "hp_trans_attack")
    for _, prop in ipairs(props) do
        attack = attack + trigger(user, prop[1], prop[2]);
    end

    -- 每穿一件装备，攻击+
    prop = combine(user, "equip_to_attrib", "attack");
    if prop[3] > 0 then
        attack = trigger(user, prop[1], prop[2] , attack);
    end

    -- --------------- 增益部分 ----------------------------------------------
    -- --------------- 主要包括状态提升，以及装备、技能的百分比提升 ----------
    -- --------------- 先计算总的加成，最后再相加（百分比计算时不叠加） ------
    addon = 0;

    -- 攻击+N%
    prop = combine(user, "attrib", "attack");
    addon = addon + apply(prop, attack) - attack;

    -- 只有一个敌人时
    prop = combine(user, "single_attrib", "attack");
    if prop[3] > 0 and 1 == #FormulaM.getAliveMonsters(true) then
        addon = addon + apply(prop, attack);
    end

    -- 只有一个敌人时（角斗士专属）
    prop = combine(user, "victor_dignity", "attack");
    if prop[3] > 0 then
        addon = addon + apply(prop, attack);
    end

    -- 损血增加攻击
    props = fetchProps(user, "hp_to_attack");
    for _, prop in ipairs(props) do
        addon = addon + trigger(user, prop[1], prop[2], attack);
    end

    -- 每损失20%的生命值时攻击百分比提升
    props = fetchProps(user, "hp_to_attack2");
    for _, prop in ipairs(props) do
        addon = addon + trigger(user, prop[1], prop[2], attack);
    end

    -- 每损失20%的生命值时攻击百分比提升（骑士精神）
    prop = combine(user, "knightship", "attack");
    if prop[3] > 0 then
        addon = addon + trigger(user, prop[1], prop[2], attack);
    end

    -- 生命高于30%则攻击增加
    props = fetchProps(user, "hp_to_attack3");
    for _, prop in ipairs(props) do
        addon = addon + trigger(user, prop[1], prop[2], attack);
    end

    -- 穿齐装备攻击百分比提升
    prop = combine(user, "equip_complete_attrib", "attack");
    if prop[3] > 0 then
        addon = addon + trigger(user, prop[1], prop[2], attack);
    end

    -- 状态增益的攻击
    prop = combine(user, "attrib3", "attack");
    addon = addon + apply(prop, attack);

    -- 静电场攻
    prop = combine(user, "attrib_elect", "attack");
    addon = addon + apply(prop, attack);

    -- 增益后的攻击
    attack = attack + addon;
    -- --------------- 减益部分 -------------------------------------------
    -- --------------- 包括各种减益 ---------------------------------------
    -- --------------- 先百分比后绝对值，百分比计算时直接叠加 -------------
    -- 被削弱
    if not args then
        -- 百分比削弱
        prop = combine(user, "reduce_attrib", "attack");
        attack = apply(prop, attack);

        -- 被诅咒了
        props = fetchProps(user, "curse");
        for _, prop in ipairs(props) do
            attack = trigger(user, prop[1], prop[2], attack);
        end

        -- 腐蚀状态
        props = fetchProps(user, "corriosion");
        for _, prop in ipairs(props) do
            attack = apply(prop, attack);
        end

        -- 震慑状态（百分比-）
        prop = combine(user, "frighten", 1);
        attack = apply(prop, attack);

        -- 电磁场力状态（百分比-）
        prop = combine(user, "electricity", 1);
        if prop[3] > 0 then
            attack = attack - trigger(user, prop[1], prop[2], attack);
        end

        -- 绝对值削弱
        prop = combine(user, "reduce_attrib2", "attack");
        attack = apply(prop, attack);
    end

    -- 攻击最小为1
    if attack < 1 then
        attack = 1;
    end

    return attack;
end
